package com.sgai.zero_robot.pop;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.sgai.zero_robot.R;
import com.sgai.zero_robot.core.RobotManagerFactory;
import com.sgai.zero_robot.core.callback.SwingAngleCallback;


public class SettingSwingAngleView extends FrameLayout implements View.OnClickListener {
    private Handler handler = new Handler(Looper.getMainLooper());
    private final String TAG = "SettingSwingAngleView";
    private SwingAngleCallback swingAngleCallback = new SwingAngleCallback() {
        @Override
        public void onZero(String ratio) {
            setValue(etZero, RobotManagerFactory.getInstance().ssw0Value);
        }

        @Override
        public void onOne(String ratio) {
            setValue(etOne, RobotManagerFactory.getInstance().ssw1Value);
        }

        @Override
        public void onTwo(String ratio) {
            setValue(etTwo, RobotManagerFactory.getInstance().ssw2Value);
        }

        @Override
        public void onThree(String ratio) {
            setValue(etThree, RobotManagerFactory.getInstance().ssw3Value);
        }
    };
    private EditText etZero,etOne,etTwo,etThree;
    private TextView btSet,btSet1,btSet2,btSet3;

    public SettingSwingAngleView(@NonNull Context context) {
        this(context,null);
    }

    public SettingSwingAngleView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,-1);
    }

    public SettingSwingAngleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    private void init(Context context){
        View view = inflate(context, R.layout.setting_swing_angle, this);
        etZero = view.findViewById(R.id.et_swing_angle_zero_content);
        etOne = view.findViewById(R.id.et_swing_angle_one_content);
        etTwo = view.findViewById(R.id.et_swing_angle_two_content);
        etThree = view.findViewById(R.id.et_swing_angle_three_content);
        btSet = view.findViewById(R.id.bt_set);
        btSet1 = view.findViewById(R.id.bt_set1);
        btSet2 = view.findViewById(R.id.bt_set2);
        btSet3 = view.findViewById(R.id.bt_set3);
        btSet.setOnClickListener(this);
        btSet1.setOnClickListener(this);
        btSet2.setOnClickListener(this);
        btSet3.setOnClickListener(this);
    }
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.e(TAG, "onAttachedToWindow: " );
        setValue(etZero, RobotManagerFactory.getInstance().ssw0Value);
        setValue(etOne, RobotManagerFactory.getInstance().ssw1Value);
        setValue(etTwo, RobotManagerFactory.getInstance().ssw2Value);
        setValue(etThree, RobotManagerFactory.getInstance().ssw3Value);
        RobotManagerFactory.getInstance().addSwingAngleCallback(swingAngleCallback);
    }
    private void setValue(EditText tv,String angle){
        if (tv!=null&&!TextUtils.isEmpty(angle)){
            tv.setText(angle);
        }
    }
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        Log.e(TAG, "onDetachedFromWindow: " );
        RobotManagerFactory.getInstance().removeSwingAngleCallback(swingAngleCallback);
    }

    @Override
    public void onClick(View v) {
        /**
         2:摆动电机角度0配置	100，500
         3:摆动电机角度1配置	100，3600
         4:摆动电机角度2配置	100，3600
         5:摆动电机角度3配置	100，3600
         */
        if (v == btSet){
            String string = etZero.getText().toString();
            if (TextUtils.isEmpty(string)) {
                Toast.makeText(getContext(), "角度不能为空！", Toast.LENGTH_SHORT).show();
                return;
            }
            int i = Integer.parseInt(string);
            if (i<100||i>500){
                Toast.makeText(getContext(), "摆动电机角度0范围在100-500之间，请确认参数！", Toast.LENGTH_SHORT).show();
                return;
            }
            RobotManagerFactory.getInstance().setSsw0(i);
        } else if (v == btSet1){
            String string = etOne.getText().toString();
            if (TextUtils.isEmpty(string)) {
                Toast.makeText(getContext(), "角度不能为空！", Toast.LENGTH_SHORT).show();
                return;
            }
            int i = Integer.parseInt(string);
            if (i<100||i>3600){
                Toast.makeText(getContext(), "摆动电机角度1范围在100-3600之间，请确认参数！", Toast.LENGTH_SHORT).show();
                return;
            }
            RobotManagerFactory.getInstance().setSsw1(i);
        }else if (v == btSet2){
            String string = etTwo.getText().toString();
            if (TextUtils.isEmpty(string)) {
                Toast.makeText(getContext(), "角度不能为空！", Toast.LENGTH_SHORT).show();
                return;
            }
            int i = Integer.parseInt(string);
            if (i<100||i>3600){
                Toast.makeText(getContext(), "摆动电机角度2范围在100-3600之间，请确认参数！", Toast.LENGTH_SHORT).show();
                return;
            }
            RobotManagerFactory.getInstance().setSsw2(i);
        }else if (v == btSet3){
            String string = etThree.getText().toString();
            if (TextUtils.isEmpty(string)) {
                Toast.makeText(getContext(), "角度不能为空！", Toast.LENGTH_SHORT).show();
                return;
            }
            int i = Integer.parseInt(string);
            if (i<100||i>3600){
                Toast.makeText(getContext(), "摆动电机角度3范围在100-3600之间，请确认参数！", Toast.LENGTH_SHORT).show();
                return;
            }
            RobotManagerFactory.getInstance().setSsw3(i);
        }
    }
}
